﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Windows.Forms;
//using System.Threading; 

using Cards.Util;
using Cards.GUI;

namespace Cards.Objects
{
    public class CBlackJackStage : CStage
    {
        protected System.Windows.Forms.Timer m_waitingTimer;

        public CBlackJackStage(INS_Stage _insStage)
            : base(_insStage)
        {
            m_waitingTimer = new System.Windows.Forms.Timer();
            m_waitingTimer.Interval = 1000;
            m_waitingTimer.Tick += new System.EventHandler(waitingTimer_Tick);
        }

        protected void waitingTimer_Tick(object sender, EventArgs e)
        {
            m_waitingTimer.Stop();
            TakeCard();
        }

        public override void Play(Boolean _fromNetwork, int _id = -1)
        {
            TakeCard(_fromNetwork);
        }

        //Take a card
        public void TakeCard(Boolean _fromNetwork = false)
        {
            if ((m_insStage[INS_Stage.sSide].PLAYERKIND == PLAYER_TYPE.COMPUTER)
                        && (CheckRule_BlackJack.CheckGoodScoreToContinue(m_insStage[INS_Stage.sSide])
                        || CheckRule_BlackJack.CheckBadOutScore(m_insStage[INS_Stage.sSide])))
            {
                if (((INS_BlackJackPlayer)m_insStage[INS_Stage.sSide]).BANKER)
                {
                    EndStage();
                }
                else
                {
                    Skip();
                }
                return;
            }

            if (!((INS_BlackJackPlayer)m_insStage[INS_Stage.sSide]).TakeACard())
            {
                Skip();
                return;
            }

            if ((m_insStage[INS_Stage.sSide].PLAYERKIND == PLAYER_TYPE.COMPUTER))
            {
                if ((CheckRule_BlackJack.CheckGoodScoreToContinue(m_insStage[INS_Stage.sSide])
                    || CheckRule_BlackJack.CheckBadOutScore(m_insStage[INS_Stage.sSide])))
                {
                    if (((INS_BlackJackPlayer)m_insStage[INS_Stage.sSide]).BANKER)
                    {
                        //EndStage();
                        m_pendingEndStage = true;
                    }
                    else
                    {
                        //Skip();
                        m_pendingSkip = true;
                    }
                }
                else
                {
                    //if (CheckRule_BlackJack.CheckEnoughScore(m_insStage[INS_Stage.sSide]))
                    //{
                    //    Skip();
                    //}
                    //else
                    {
                        m_waitingTimer.Start();
                        m_waitingTimer.Enabled = true;
                    }
                }
            }
        }

        public override void Skip(Boolean _fromNetwork = false, int _id = -1)
        {
            ((INS_BlackJackPlayer)m_insStage[INS_Stage.sSide]).SkipPlaying();
            GiveAwayTurn();
        }

        //Next player
        public void GiveAwayTurn()
        {
            IS[INS_Stage.sSide].EndTurn();
            INS_Stage.sSide++;
            if (INS_Stage.sSide == IS.NUMPLAYERS)
            {
                EndStage();
                return;
            }
            IS[INS_Stage.sSide].ReceiveTurn();
        }

        public void EndStage()
        {
            m_insStage.EndStage();
        }
    }
}
